package com.example.as_sum.car;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.as_sum.R;
import com.example.as_sum.activity.BaseActivity;
import com.example.as_sum.entity.ShopInfo;
import com.example.as_sum.event.ShopCarItemEvent;
import com.example.as_sum.util.Log;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopCarActivity extends BaseActivity {

    @Bind(R.id.shopcar_recyclerview)
    RecyclerView mRecyclerview;
    @Bind(R.id.shopcar_img_check_all)
    CheckBox img_all;
    @Bind(R.id.shopcar_price_sum)
    TextView tv_pricesum;
    @Bind(R.id.shopcar_price_xx)
    TextView tv_pricexx;

    ShopCarAdapter adapter;
    List<ShopInfo> shopInfos;

    int Shop_Sum_Price, Shop_Sum_Count, shop_sum, shop_count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitle("购物车");
        EventBus.getDefault().register(this);
    }

    @Override
    public int getContentId() {
        return R.layout.activity_shop_car;
    }
    @Subscribe
    public void onEventMainThread(ShopCarItemEvent event) {
        if (event.isState()) {
            shop_sum += event.getPrice();
            shop_count += event.getCount();
            if (shop_count == Shop_Sum_Count) {
                img_all.setChecked(true);
            }
        } else {
            shop_sum -= event.getPrice();
            shop_count -= event.getCount();
            img_all.setChecked(false);
        }
        Log.e("------>", "个数:" + shop_count + ",价钱:" + shop_sum);
        updateText();
    }

    private void updateText() {
        tv_pricesum.setText(shop_sum + "￥");
        tv_pricexx.setText("共" + shop_count + "件");
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        shopInfos = getData();
        adapter = new ShopCarAdapter(this, shopInfos);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerview.setAdapter(adapter);
    }

    private List<ShopInfo> getData() {
        List<ShopInfo> list = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            ShopInfo shopInfo = new ShopInfo();
            shopInfo.setSid(100 + i);
            shopInfo.setPrice(i * 5);
            shopInfo.setSinfo("这是测试的标题哦哦哦" + i);
            shopInfo.setSname("名字" + i);
            shopInfo.setCount(i);
            list.add(shopInfo);
            Shop_Sum_Count += i;
            Shop_Sum_Price += i * 5;
        }
        Log.e("------>", "总个数:" + Shop_Sum_Count + ",总价钱:" + Shop_Sum_Price);
        return list;
    }

    @Override
    public void addListener() {

    }

    @Override
    public void loadData() {

    }

    @Override
    public Handler getHandler() {
        return null;
    }

    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @OnClick({R.id.shopcar_img_check_all,R.id.btn_pay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shopcar_img_check_all:
                if (img_all.isChecked()) {
                    for (ShopInfo shopInfo : shopInfos) {
                        shopInfo.setState(true);
                    }
                    adapter.update(shopInfos);
                    shop_sum = Shop_Sum_Price;
                    shop_count = Shop_Sum_Count;
                    Log.e("---全选--->", "个数:" + shop_count + ",价钱:" + shop_sum);
                } else {
                    for (ShopInfo shopInfo : shopInfos) {
                        shopInfo.setState(false);
                    }
                    adapter.update(shopInfos);
                    shop_sum = 0;
                    shop_count = 0;
                    Log.e("---全不选--->", "个数:" + shop_count + ",价钱:" + shop_sum);
                }
                updateText();
                break;
            case R.id.btn_pay:
                Random random = new Random();
                int index = random.nextInt(1000);
                showMsg("你单机了"+index+"次");
                break;
        }
    }
    Toast mToast;
    private void showMsg(String msg){
        if(mToast!=null){
            mToast.setText(msg);
        }else{
            mToast=Toast.makeText(ShopCarActivity.this, "你单机了这个按钮", Toast.LENGTH_SHORT);
        }
        mToast.show();
    }
}
